package com.example.xinhuazidian

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.xinhuazidian.utils.JsonFileUtil
import com.example.xinhuazidian.adapter.SZJAdapter
import com.example.xinhuazidian.bean.Sanzijing
import com.example.xinhuazidian.databinding.ActivitySzjactivityBinding
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar

/**
 * 三字经
 */
class SZJActivity : AppCompatActivity() {

    private lateinit var binding: ActivitySzjactivityBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySzjactivityBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView()
    }

    private fun initView() {
        //沉浸状态栏
        ImmersionBar.with(this)
            .statusBarDarkFont(true)//状态栏字体是深色，不写默认为亮色
            .init()

        val fileName = intent.getStringExtra("fileName")
        if (fileName != null) {
            load(fileName)
        }
    }

    @SuppressLint("SetTextI18n")
    private fun load(fileName: String) {
        val str = JsonFileUtil.loadFile(this, fileName)

        val bean: Sanzijing = Gson().fromJson(str, Sanzijing::class.java)
        initAdapter(bean.paragraphs)

        binding.tvTitle.text = bean.title
        if (bean.tags != null) {
            binding.tvAuthor.text = "${bean.tags}·${bean.author}"
        } else {
            binding.tvAuthor.text = bean.author
        }
    }

    private fun initAdapter(list: MutableList<String>) {
        //设置布局排列方式，默认垂直排列
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rv.layoutManager = linearLayoutManager

        //加载适配器
        val szjAdapter = SZJAdapter(list)
        binding.rv.adapter = szjAdapter
    }
}